﻿
import javax.servlet.ServletOutputStream
import java.awt.image.BufferedImage
import com.sun.image.codec.jpeg.JPEGImageEncoder
import com.sun.image.codec.jpeg.JPEGCodec
import groovy.text.SimpleTemplateEngine

class ChartController extends BaseControllerBase {
    SqlService sqlService;

    def index = {};
    def offersNumPerDate = {[]}
    def priceByRoomsByDate = {[]}
    def priceDynamicByRoomsByDate = {[]}
    def getOfferStatistic = {
        def externalId = params['externalId'];
        def engine = new SimpleTemplateEngine();
        File gtpl = getSqlTemplateFile("priceListByExternalId");
        def binding = ["externalId": externalId];
        def template = engine.createTemplate(gtpl).make(binding);
        println("Completed template binding... executing sql query...");
        def offersList = (ArrayList) sqlService.executeQueryMap(template.toString());

        ServletOutputStream out = response.getOutputStream();
        ChartEngine chart = new ChartEngine();
        chart.setTitle("offers per date");
        chart.setYAxisName("offers num");
        chart.setXAxisName("Date");
        chart.setBaseShapesFilled(true);
        chart.setBaseShapesVisible(true); 
        chart.convertToDataSet(externalId, offersList);
        println("");
        println("Getting chart image...");
        BufferedImage bImage = chart.getChartImage(400,250);
        println("Chart image received!");
        try {
            JPEGImageEncoder jc = JPEGCodec.createJPEGEncoder(out);
            jc.encode(bImage);
            out.close();
            println("Push chart on th web tair.");
        }
        catch (IOException ioe) {
        }
    }
       def offersNumPerDateImg = {
           Map combinedStatisticMap = new LinkedHashMap();
           String realtyType = params['realtyType'];
           def engine = new SimpleTemplateEngine();
           File gtpl = getSqlTemplateFile("offersNumPerDate");
           def binding = ["realtyType":realtyType];
           def template = engine.createTemplate(gtpl).make(binding);
           println("Completed template binding... executing sql query...");
           def offersList = (ArrayList) sqlService.executeQueryMap(template.toString());
           combinedStatisticMap.put("Offers num", offersList);
           ServletOutputStream out = response.getOutputStream();
           ChartEngine chart = new ChartEngine();
           chart.setTitle("offers per date");
           chart.setYAxisName("offers num");
           chart.setXAxisName("Date");
           chart.fillDataset(combinedStatisticMap, 'year', 'month', 'day', 'count');
           println("");
           println("Getting chart image...");
           BufferedImage bImage = chart.getChartImage(1000,500);
           println("Chart image received!");
           try {
               JPEGImageEncoder jc = JPEGCodec.createJPEGEncoder(out);
               jc.encode(bImage);
               out.close();
               println("Push chart on th web tair.");
           }
           catch (IOException ioe) {
           }

       }
       def priceByRoomsByDateImg = {
           Map combinedStatisticMap = new LinkedHashMap();
           List combinedStatisticList = new ArrayList();
           def engine = new SimpleTemplateEngine();
           File gtpl = getSqlTemplateFile("priceByRoomsByDate");
           for(int rooms = 1; rooms < 5; rooms++){
                def binding = ["rooms": rooms];
                def template = engine.createTemplate(gtpl).make(binding);
                println("Completed template binding... executing sql query... for rooms="+rooms);
                def offersList = (ArrayList) sqlService.executeQueryMap(template.toString());
                combinedStatisticMap.put(rooms.toString(), offersList);
           }

           ServletOutputStream out = response.getOutputStream();
           ChartEngine chart = new ChartEngine();
           chart.setTitle("Price by Rooms");
           chart.setYAxisName("Price \$");
           chart.setXAxisName("Date");
           chart.fillDataset(combinedStatisticMap, 'year', 'month', 'day', 'price');
           println("");
           println("Getting chart image...");
           BufferedImage bImage = chart.getChartImage(1000,500);
           println("Chart image received!");
           try {
               JPEGImageEncoder jc = JPEGCodec.createJPEGEncoder(out);
               jc.encode(bImage);
               out.close();
               println("Push chart on th web tair.");
           }
           catch (IOException ioe) {
           }

       }
       def priceDynamicByRoomsByDateImg = {
           List combinedStatisticList = new ArrayList();
           Map combinedStatisticMap = new LinkedHashMap();
           def engine = new SimpleTemplateEngine();
           File gtpl = getSqlTemplateFile("priceDynamicByRoomsByDate");

           for(int rooms = 1; rooms < 5; rooms++){
                def binding = ["rooms": rooms];
                def template = engine.createTemplate(gtpl).make(binding);
                println("Completed template binding... executing sql query... for rooms="+rooms);
                def offersList = (ArrayList) sqlService.executeQueryMap(template.toString());
               combinedStatisticMap.put(rooms.toString(), offersList);

           }
           ServletOutputStream out = response.getOutputStream();
           ChartEngine chart = new ChartEngine();
           chart.setTitle("Price dinamics by Rooms");
           chart.setYAxisName("Price changes \$");
           chart.setXAxisName("Date");
           chart.fillDataset(combinedStatisticMap, 'year', 'month', 'day', 'price');
           println("");
           println("Getting chart image...");
           BufferedImage bImage = chart.getChartImage(1000,500);
           println("Chart image received!");
           try {
               JPEGImageEncoder jc = JPEGCodec.createJPEGEncoder(out);
               jc.encode(bImage);
               out.close();
               println("Push chart on th web tair.");
           }
           catch (IOException ioe) {
           }

       }
}